package com.kitbox.hl.meshow.code.controller;


import com.kitbox.hl.meshow.code.factory.DefaultTemplateFactory;
import com.kitbox.hl.meshow.code.service.TableService;
import com.kitbox.hl.meshow.generator.action.config.WebGeneratorConfig;
import com.kitbox.hl.meshow.generator.action.model.GenQo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 代码生成控制器
 *
 * @author fengshuonan
 * @Date 2017年11月30日16:39:19
 */
@Controller
@RequestMapping("/code")
public class CodeController  {

    @Autowired
    private TableService tableService;


    /**
     * 跳转到代码生成主页
     */
    @RequestMapping("")
    public String blackboard(Model model) {
        model.addAttribute("tables", tableService.getAllTables());
        model.addAttribute("params", DefaultTemplateFactory.getDefaultParams());
        model.addAttribute("templates", DefaultTemplateFactory.getDefaultTemplates());
        return  "generateCode";
    }

    /**
     * 生成代码
     */

    @RequestMapping(value = "/generate", method = RequestMethod.POST)
    @ResponseBody
    public Object generate(GenQo genQo) {
        if (genQo.getUrl()==null)
            genQo.setUrl("url");
        if (genQo.getUserName()==null)
            genQo.setUserName("");
        if (genQo.getPassword()==null)
            genQo.setPassword("");
        WebGeneratorConfig webGeneratorConfig = new WebGeneratorConfig(genQo);
        webGeneratorConfig.doMpGeneration();
        webGeneratorConfig.doGunsGeneration();
        return null;
    }
}
